package com.relax.pool;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class PoolDemo_tiaokunlun_futureTask_cancle {
    public static void main(String[] args) {
        ThreadPoolExecutor.DiscardPolicy abortPolicy = new ThreadPoolExecutor.DiscardPolicy();
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 5, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1), Executors.defaultThreadFactory(),abortPolicy);
        boolean b = pool.allowsCoreThreadTimeOut();
        System.out.println("b==" +b);
//        pool.allowCoreThreadTimeOut(true);  // 是否允许核心线程数销毁

//        pool.prestartCoreThread(); //todo  会开启一个核心线程
//        pool.prestartAllCoreThreads();//todo  会开启所有核心线程
        /**
         * RUNNING    = -1 << COUNT_BITS;
         * SHUTDOWN   =  0 << COUNT_BITS;
         * STOP       =  1 << COUNT_BITS;
         * TIDYING    =  2 << COUNT_BITS;
         * TERMINATED =  3 << COUNT_BITS;
         */

        System.out.println("RUNNING==" + (-1 << 29));
        System.out.println("SHUTDOWN==" + (0 << 29));
        System.out.println("STOP==" + (1 << 29));
        System.out.println("TIDYING==" + (2 << 29));
        System.out.println("TERMINATED==" + (3 << 29));

        long curent = System.currentTimeMillis();

        for (int i = 1; i <= 10; i++) {

            pool.execute( () ->{
                System.out.println(System.currentTimeMillis() +"线程"+Thread.currentThread().getName()+"开始执行");
                try {
                    Thread.sleep(1000000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            });
            int corePoolSize = pool.getCorePoolSize();
            int activeCount = pool.getActiveCount();
            long taskCount = pool.getTaskCount();
            System.out.println("提交第"+i+"个任务");
            System.out.println("线程池:corePoolSize:"+corePoolSize+",activeCount:"+activeCount+",taskCount:"+taskCount);
        }


        System.out.println("开始测试submit任务");

        List<Future<String>> retuls = new ArrayList<>();


        for (int i = 0; i < 100000000; i++) {
            System.out.println("开始测试submit任务:"+i);
            Future<String> submit = pool.submit(() -> {
                System.out.println(System.currentTimeMillis() + "线程" + Thread.currentThread().getName() + "开始执行");
                return Thread.currentThread().getName();

            });
            retuls.add(submit);

        }


        for (Future<String> submit : retuls) {
            try {
                boolean done = submit.isDone();
                boolean cancelled = submit.isCancelled();
                String value = submit.get();
                System.out.println("线程池返回的结果是"+value);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        }



        pool.shutdown();



    }
}
